package partition;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

import java.util.HashMap;

public class CityPartitioner extends Partitioner<Text, IntWritable> {

    public static HashMap<String,Integer> cityMap= new HashMap<String, Integer>();
    static {
        cityMap.put("北京",0);
        cityMap.put("上海",1);
        cityMap.put("广州",2);
        cityMap.put("深圳",3);
        cityMap.put("天津",4);
        cityMap.put("武汉",5);
        cityMap.put("南京",6);
    }

    @Override
    public int getPartition(Text key, IntWritable value, int i) {
        //根据  map  输出的key  得到分区号
        Integer code = cityMap.get(key.toString().split("_")[0]);

        if(code != null){
            return code;
        }else{
            //如果key不在数据字典里面，分到其它州结果文件中
            return 7;
        }
    }

}
